/**
 * 
 */
package com.google.gwt.indexeddb.client;

import com.google.gwt.core.client.JavaScriptException;
import com.google.gwt.core.client.JavaScriptObject;

/**
 * The IDBTransaction interface of the IndexedDB API provides a static
 * asynchronous transaction on a database.
 * 
 * @author Chan Le <gwt /at/ chanvn.com>
 * @see <a href="http://www.w3.org/TR/IndexedDB/#idl-def-IDBTransaction">W3C
 *      IDBTransaction</a>
 */
public class IDBTransaction extends JavaScriptObject {

  /**
   * Modification operations are not allowed in this transaction
   */
  public static final int READ_ONLY = 0;
  public static final int READ_ONLY_CHROME = 1;

  /**
   * Modification operations are allowed in this transaction.
   */
  public static final int READ_WRITE = 1;
  public static final int READ_WRITE_CHROME = 0;

  /**
   * This mode is used for only for updating the version number of transactions
   * that were started using the setVersion() method of IDBDatabase objects.
   */
  public static final int VERSION_CHANGE = 2;

  protected IDBTransaction() {
  }

  /**
   * Returns immediately, and undoes all the changes to objects in the database
   * associated with this transaction. If this transaction has been aborted or
   * completed, then this method throws an error event, with its code set to
   * ABORT_ERR and a suitable message.
   * 
   * @see <a
   *      href="http://www.w3.org/TR/IndexedDB/#widl-IDBTransaction-abort">W3C
   *      IDBTransaction-abort</a>
   */
  public native final void abort()/*-{
		this.abort();
  }-*/;

  /**
   * @return The mode for isolating access to data in the object stores that are
   *         in the scope of the transaction. For possible values, see
   *         Constants. The default value is READ_ONLY.
   * @see <a href="http://www.w3.org/TR/IndexedDB/#widl-IDBTransaction-mode">W3C
   *      IDBTransaction-mode</a>
   */
  public native final int getMode() /*-{
		return this.mode;
  }-*/;

  /**
   * @return The database connection that this transaction is associated with.
   * @see <a href="http://www.w3.org/TR/IndexedDB/#widl-IDBTransaction-db">W3C
   *      IDBTransaction-db</a>
   */
  public native final IDBDatabase getDb() /*-{
		return this.db;
  }-*/;

  /**
   * Returns an object store that has already been added to the scope of this
   * transaction. Every call to this method on the same transaction object, with
   * the same name, returns the same IDBObjectStore instance. If this method is
   * called on a different transaction object, a different IDBObjectStore
   * instance is returned.
   * 
   * @param name The name of the requested object store.
   * @return {@link IDBObjectStore} An object for accessing the requested object
   *         store.
   * @throws IDBDatabaseException
   * @see <a
   *      href="http://www.w3.org/TR/IndexedDB/#widl-IDBTransaction-objectStore">W3C
   *      IDBTransaction-objectStore</a>
   */
  public final IDBObjectStore objectStore(String name)
      throws IDBDatabaseException {
    try {
      return objectStore0(name);
    } catch (JavaScriptException e) {
      throw new IDBDatabaseException(e.getName(), e);
    }
  }

  public native final IDBObjectStore objectStore0(String name0)/*-{
		return this.objectStore(name0);
  }-*/;

  /**
   * @param handler The event handler for the abort event
   * @see <a
   *      href="http://www.w3.org/TR/IndexedDB/#widl-IDBTransaction-onabort">W3C
   *      IDBTransaction-onabort</a>
   */
  public final native void setOnAbort(IDBTransactionHandler handler) /*-{
		this.onabort = $entry(function(e) {
			handler.@com.google.gwt.indexeddb.client.IDBTransactionHandler::onAbort(Lcom/google/gwt/indexeddb/client/IDBEvent;)(e);
		});
  }-*/;

  /**
   * @param handler The event handler for the complete event
   * @see <a
   *      href="http://www.w3.org/TR/IndexedDB/#widl-IDBTransaction-oncomplete">W3C
   *      IDBTransaction-oncomplete</a>
   */
  public final native void setOnComplete(IDBTransactionHandler handler) /*-{
		this.oncomplete = $entry(function(e) {
			handler.@com.google.gwt.indexeddb.client.IDBTransactionHandler::onComplete(Lcom/google/gwt/indexeddb/client/IDBEvent;)(e);
		});
  }-*/;

  /**
   * @param handler The event handler for the error event
   * @see <a
   *      href="http://www.w3.org/TR/IndexedDB/#widl-IDBTransaction-onerror">W3C
   *      IDBTransaction-onerror</a>
   */
  public final native void setOnError(IDBTransactionHandler handler)/*-{
		this.onerror = $entry(function(e) {
			handler.@com.google.gwt.indexeddb.client.IDBTransactionHandler::onError(Lcom/google/gwt/indexeddb/client/IDBEvent;)(e);
		});
  }-*/;

}
